<template>
  <div class="food-wheel-container">
    <div class="wheel-header">
      <h1>今天吃什么？</h1>
      <div class="input-area">
        <input 
          v-model="newFood" 
          @keyup.enter="addFood"
          placeholder="输入食物名称"
          class="food-input"
        >
        <button @click="addFood" class="add-btn">添加</button>
        <button @click="fetchRandomFood" class="fetch-btn">随机推荐</button>
      </div>
    </div>

    <div class="wheel-wrapper">
      <div 
        class="wheel" 
        :style="wheelStyle"
        :class="{ spinning: isSpinning }"
        ref="wheel"
      >
        <div 
          v-for="(food, index) in foods" 
          :key="index" 
          class="wheel-item"
          :style="getItemStyle(index)"
        >
          <span class="food-name">{{ food }}</span>
        </div>
        <div class="wheel-center">
          <div class="inner-circle"></div>
        </div>
      </div>
      
      <div class="pointer"></div>
      <div class="sparkle" v-if="isSpinning"></div>
    </div>

    <button @click="spinWheel" class="spin-btn" :disabled="isSpinning || foods.length < 2">
      {{ isSpinning ? '转盘中...' : '开始选择' }}
    </button>

    <div v-if="selectedFood" class="result">
      <h2>今天吃: <span class="highlight">{{ selectedFood }}</span></h2>
    </div>

    <div class="food-list">
      <h3>食物列表 ({{ foods.length }})</h3>
      <ul>
        <li v-for="(food, index) in foods" :key="index">
          {{ food }}
          <button @click="removeFood(index)" class="remove-btn">×</button>
        </li>
      </ul>
      <button @click="resetFoods" class="clear-btn">清空列表</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      foods: ['汉堡', '披萨', '寿司', '拉面', '沙拉', '牛排', '火锅', '炒饭'],
      newFood: '',
      isSpinning: false,
      selectedFood: '',
      spinDeg: 0
    };
  },
  computed: {
    wheelStyle() {
      return {
        transform: `rotate(${this.spinDeg}deg)`,
        transition: this.isSpinning ? 'transform 3s cubic-bezier(0.17, 0.85, 0.32, 0.99)' : 'none'
      };
    }
  },
  methods: {
    addFood() {
      if (this.newFood.trim() && !this.foods.includes(this.newFood.trim())) {
        this.foods.push(this.newFood.trim());
        this.newFood = '';
      }
    },
    removeFood(index) {
      this.foods.splice(index, 1);
      this.selectedFood = '';
    },
    resetFoods() {
      this.foods = [];
      this.selectedFood = '';
      this.spinDeg = 0;
    },
    getItemStyle(index) {
      const segmentAngle = 360 / this.foods.length;
      const angle = segmentAngle * index + segmentAngle / 2; // 让文本居中
      return {
        transform: `rotate(${angle}deg)`,
        backgroundColor: this.getColor(index),
        '--item-angle': `${segmentAngle}deg`
      };
    },
    getColor(index) {
      const colors = [
        '#FF9AA2', '#FFB7B2', '#FFDAC1', '#E2F0CB', 
        '#B5EAD7', '#C7CEEA', '#F8B195', '#F67280',
        '#C06C84', '#6C5B7B', '#355C7D', '#A8E6CE',
        '#DCEDC2', '#FFD3B5', '#FFAAA6', '#FF8C94'
      ];
      return colors[index % colors.length];
    },
    spinWheel() {
      if (this.foods.length < 2) return;
      
      this.isSpinning = true;
      this.selectedFood = '';
      
      const spins = 5 + Math.floor(Math.random() * 5); // 5-10圈
      const segmentAngle = 360 / this.foods.length;
      const randomSegment = Math.floor(Math.random() * this.foods.length);
      const randomOffset = Math.random() * segmentAngle; // 增加微调随机性
      const finalRotation = (randomSegment * segmentAngle) + randomOffset;
      
      this.spinDeg += (360 * spins) + finalRotation; // 让最终角度对准选中的食物
      
      setTimeout(() => {
        this.isSpinning = false;
        this.selectFood();
      }, 3000);
    },
    selectFood() {
      const normalizedDeg = this.spinDeg % 360;
      const segmentAngle = 360 / this.foods.length;
      const selectedIndex = Math.floor(((360 - normalizedDeg) % 360) / segmentAngle);
      this.selectedFood = this.foods[selectedIndex];
    },
    async fetchRandomFood() {
      try {
        const mockFoods = ['寿司', '披萨', '汉堡', '拉面', '咖喱', '烤肉', '火锅', '沙拉'];
        const newFood = mockFoods[Math.floor(Math.random() * mockFoods.length)];
        
        if (!this.foods.includes(newFood)) {
          this.foods.push(newFood);
        }
      } catch (error) {
        console.error('获取随机食物失败:', error);
      }
    }
  }
};
</script>

<style scoped>
/* 中心圆圈动画 */
.wheel-center {
  position: absolute;
  width: 60px;
  height: 60px;
  background: linear-gradient(135deg, #fff 0%, #f5f5f5 100%);
  border-radius: 50%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
}

.inner-circle {
  width: 20px;
  height: 20px;
  background: linear-gradient(135deg, #ff6b6b 0%, #ff8e8e 100%);
  border-radius: 50%;
  animation: pulse 1.5s infinite alternate;
}

/* 闪光效果 */
.sparkle {
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255,255,255,0.8) 0%, rgba(255,255,255,0) 70%);
  top: 0;
  left: 0;
  animation: sparkle 1s infinite alternate;
  pointer-events: none;
  z-index: 5;
}

@keyframes sparkle {
  from {
    opacity: 0.3;
    transform: scale(0.95);
  }
  to {
    opacity: 0.7;
    transform: scale(1.05);
  }
}

/* 更流畅的旋转动画 */
.wheel {
  transition: transform 0.2s ease-out;
}

.wheel.spinning {
  transition: transform 3.5s cubic-bezier(0.06, 0.89, 0.17, 0.99);
}

/* 旋转时的光晕效果 */
@keyframes wheel-glow {
  0% {
    box-shadow: 0 0 0 10px #fff, 
                0 0 0 12px #ff9a8b, 
                0 0 20px rgba(255, 107, 107, 0.5);
  }
  50% {
    box-shadow: 0 0 0 10px #fff, 
                0 0 0 12px #ff9a8b, 
                0 0 40px rgba(255, 107, 107, 0.9);
  }
  100% {
    box-shadow: 0 0 0 10px #fff, 
                0 0 0 12px #ff9a8b, 
                0 0 20px rgba(255, 107, 107, 0.5);
  }
}

.wheel.spinning {
  animation: wheel-glow 1.5s infinite ease-in-out;
}

/* 指针动画增强 */
.pointer {
  animation: bounce 0.8s infinite alternate;
}

@keyframes bounce {
  0% {
    transform: translateX(-50%) translateY(0);
  }
  100% {
    transform: translateX(-50%) translateY(-5px);
  }
}

.food-wheel-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  text-align: center;
  font-family: 'Arial', sans-serif;
  background: linear-gradient(135deg, #f5f7fa 0%, #ffeef8 100%);
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.wheel-header {
  margin-bottom: 30px;
}

.wheel-header h1 {
  color: #ff6b6b;
  font-size: 2.5rem;
  margin-bottom: 20px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
}

.input-area {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-bottom: 20px;
}

.food-input {
  padding: 10px 15px;
  border: 2px solid #ff9a8b;
  border-radius: 30px;
  font-size: 1rem;
  width: 200px;
  outline: none;
  transition: all 0.3s;
}

.food-input:focus {
  border-color: #ff6b6b;
  box-shadow: 0 0 0 3px rgba(255, 107, 107, 0.2);
}

.add-btn, .fetch-btn, .spin-btn, .clear-btn {
  padding: 10px 20px;
  border: none;
  border-radius: 30px;
  font-size: 1rem;
  cursor: pointer;
  transition: all 0.3s;
  font-weight: bold;
}

.add-btn {
  background-color: #4cc9f0;
  color: white;
}

.add-btn:hover {
  background-color: #3aa8d8;
  transform: translateY(-2px);
}

.fetch-btn {
  background-color: #f8961e;
  color: white;
}

.fetch-btn:hover {
  background-color: #e07b0c;
  transform: translateY(-2px);
}

.wheel-wrapper {
  position: relative;
  width: 400px;
  height: 400px;
  margin: 0 auto 40px;
}

.wheel {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  position: relative;
  overflow: hidden;
  box-shadow: 0 0 0 10px #fff, 0 0 0 12px #ff9a8b, 0 0 30px rgba(0, 0, 0, 0.2);
  background-color: #fff;
}

.wheel-item {
  position: absolute;
  width: 50%;
  height: 50%;
  left: 0;
  top: 0;
  transform-origin: right bottom;
  display: flex;
  align-items: center;
  justify-content: center;
  clip-path: polygon(0 0, 100% 0, 100% 100%);
}

.food-name {
  transform: rotate(calc(var(--item-angle) / 2 + 90deg));
  color: white;
  font-weight: bold;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
  font-size: 1.2rem;
  position: absolute;
  left: 60px;
  width: 100px;
  text-align: left;
}

.wheel-center {
  position: absolute;
  width: 50px;
  height: 50px;
  background-color: #fff;
  border-radius: 50%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  z-index: 10;
}

.pointer {
  position: absolute;
  top: -20px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 20px solid transparent;
  border-right: 20px solid transparent;
  border-top: 40px solid #ff6b6b;
  z-index: 20;
  filter: drop-shadow(0 0 5px rgba(0, 0, 0, 0.3));
}

.spin-btn {
  background-color: #ff6b6b;
  color: white;
  font-size: 1.2rem;
  padding: 12px 30px;
  margin-bottom: 30px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.spin-btn:hover:not(:disabled) {
  background-color: #ff5252;
  transform: translateY(-2px);
  box-shadow: 0 6px 8px rgba(0, 0, 0, 0.15);
}

.spin-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.result {
  margin: 20px 0;
}

.highlight {
  color: #ff6b6b;
  font-size: 2rem;
  text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.2);
}

.food-list {
  background-color: rgba(255, 255, 255, 0.8);
  border-radius: 15px;
  padding: 20px;
  margin-top: 30px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
}

.food-list h3 {
  color: #ff6b6b;
  margin-bottom: 15px;
}

.food-list ul {
  list-style: none;
  padding: 0;
  max-height: 200px;
  overflow-y: auto;
  margin-bottom: 15px;
}

.food-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 15px;
  margin: 5px 0;
  background-color: white;
  border-radius: 20px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
}

.remove-btn {
  background-color: #ff6b6b;
  color: white;
  border: none;
  width: 25px;
  height: 25px;
  border-radius: 50%;
  cursor: pointer;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

.remove-btn:hover {
  background-color: #ff5252;
  transform: scale(1.1);
}

.clear-btn {
  background-color: #f8f9fa;
  color: #ff6b6b;
  border: 2px solid #ff6b6b;
}

.clear-btn:hover {
  background-color: #ff6b6b;
  color: white;
}

/* 旋转动画 */
.spinning {
  animation: spin 3s cubic-bezier(0.17, 0.85, 0.32, 0.99) forwards;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(var(--spin-deg));
  }
}

/* 响应式设计 */
@media (max-width: 600px) {
  .wheel-wrapper {
    width: 300px;
    height: 300px;
  }
  
  .food-name {
    font-size: 1rem;
    left: 40px;
  }
  
  .input-area {
    flex-direction: column;
    align-items: center;
  }
  
  .food-input {
    width: 80%;
  }
}
</style>